<?php

use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitBrokerageRecord extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('agent_records');

        // 
        Schema::create('brokerage_records', function (Blueprint $table) {

            $table->comment = '分销佣金记录表';

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('user_id')->index()->comment('得到佣金的用户的id'); // 用户id 所得佣金

            $table->integer('order_user_id')->index()->comment('下订单的用户'); // 用户id 所得佣金

            $table->integer('order_id')->index()->nullable()->comment('订单id'); // 用户id
            $table->char('order_number', 20)->index()->nullable()->comment('订单编号'); // 用户id
            $table->integer('order_sku_id')->index()->nullable()->comment('订单sku id'); // 用户id

            $table->integer('money')->unsigned()->nullable()->comment('佣金（人民币)'); // 一级代理佣金
            $table->integer('score')->unsigned()->nullable()->comment('佣金(积分)'); // 一级代理佣金


            $table->float('money_ratio')->nullable()->comment('佣金率(人民币)'); // 一级代理佣金
            $table->float('score_ratio')->nullable()->comment('佣金率(积分)'); // 一级代理佣金

            $table->integer('money_limit')->nullable()->comment('上限(人民币)'); // 一级代理佣金
            $table->integer('score_limit')->nullable()->comment('上限(积分)'); // 一级代理佣金

            $table->tinyInteger('invite_level')->unsigned()->default(0)->index()->comment('中间间隔人数 0为直接邀请 1为间接隔1人'); //

            $table->integer('activity_id')->index()->nullable()->comment('活动id，盲盒id、一番赏id、抽奖id、扭蛋机id'); // 用户id
            $table->char('activity_type', 20)->index()->nullable()->comment('对应活动类型, seckill groupon 等');
            $table->uuid('activity_uuid')->nullable()->comment('活动uuid');

            $table->integer('node_id')->nullable()->index()->comment('活动id，盲盒id、一番赏id、抽奖id、扭蛋机id'); // 用户id
            $table->char('node_type', 20)->nullable()->index()->comment('对应实体类型,  product box yifanshang lottery egg_lottery');
            $table->uuid('node_uuid')->nullable()->comment('活动uuid');

            $table->char('order_type', 20)->index()->comment('佣金(积分)'); // 一级代理佣金

            $table->timestamp('order_paid_at')->nullable()->comment('订单支付时间'); // 一级代理佣金
            $table->timestamp('completed_at')->nullable()->comment('佣金发放时间'); // 一级代理佣金

            $table->tinyInteger('status')->unsigned()->default(0)->index()->comment('0待订单完成, 1已发放收益, 2为订单取消'); //

            $table->softDeletes();
            $table->timestamps();
        });


        Schema::table('orders', function (Blueprint $table) {
            $table->tinyInteger('brokerage_status')->unsigned()->default(0)->index()->comment('0为未处理 1为无需发放佣金 2为已创建佣金记录待完成 3为已发放佣金  4为已取消关闭佣金 '); // 用户id
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('brokerage_records');

        Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn(['brokerage_status']);
        });
    }
}
